package com.swotplus.system.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.swotplus.common.constant.UserConstants;
import com.swotplus.system.sys.entity.SysDept;
import com.swotplus.system.sys.mapper.SysDeptMapper;
import com.swotplus.system.sys.service.SysDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author hugang
 * @since 2020-04-12
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
        @Autowired
        private SysDeptMapper  sysDeptMapper;

        @Override
        public List<Map<String, Object>> selectDeptTree(SysDept dept) {
                List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
                QueryWrapper<SysDept> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("del_flag","0");
                List<SysDept> deptList = sysDeptMapper.selectList(queryWrapper);
                trees = getTrees(deptList, false, null);
                return trees;
        }


        /**
         * 对象转部门树
         *
         * @param deptList 部门列表
         * @param isCheck 是否需要选中
         * @param roleDeptList 角色已存在菜单列表
         * @return
         */
        public List<Map<String, Object>> getTrees(List<SysDept> deptList, boolean isCheck, List<String> roleDeptList)
        {

                List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
                for (SysDept dept : deptList)
                {
                        if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
                        {
                                Map<String, Object> deptMap = new HashMap<String, Object>();
                                deptMap.put("id", dept.getId());
                                deptMap.put("pId", dept.getParentId());
                                deptMap.put("name", dept.getDeptName());
                                deptMap.put("title", dept.getDeptName());
                                if (isCheck)
                                {
                                        deptMap.put("checked", roleDeptList.contains(dept.getId() + dept.getDeptName()));
                                }
                                else
                                {
                                        deptMap.put("checked", false);
                                }
                                trees.add(deptMap);
                        }
                }
                return trees;
        }

}
